package com.cy.dda.web.comm;

import com.cy.dda.model.dto.comm.OSSFileDownloadDTO;
import com.cy.dda.service.comm.FileDownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.cy.dda.model.Result;
import com.cy.dda.service.comm.FileUploadService;
import com.cy.dda.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 公用方法-文件管理
 *
 * @author: tanyilin
 * @create: 2018年11月16日 上午10:29:22
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Api(tags = "公用方法-文件管理")
public class FileController extends BaseController {

	@Autowired
	private FileUploadService fileUploadService;
	@Autowired
	private FileDownloadService fileDownloadService;
	
	@ApiOperation("上传图片文件")
	@PostMapping("/upload/image")
	public Result uploadImage(@RequestParam(value = "file") MultipartFile file) {
		Result result = fileUploadService.uploadImage(file);
		return result;
	}
	
	@ApiOperation("上传视频文件")
	@PostMapping("/upload/video")
	public Result uploadVideo(@RequestParam(value = "file") MultipartFile file) {
		Result result = fileUploadService.uploadVideo(file);
		return result;
	}

	@ApiOperation("上传音频文件")
	@PostMapping("/upload/mp3")
	public Result uploadMP3(@RequestParam(value = "file") MultipartFile file) {
		Result result = fileUploadService.uploadMP3(file);
		return result;
	}
	
	@ApiOperation("测试分片上传视频文件")
	@PostMapping("/multipart/upload/video")
	public Result multipartUploadVideo(@RequestParam MultipartFile file) {
		Result result = fileUploadService.multipartUploadVideo(file);
		return result;
	}
	
	@ApiOperation("测试获取访问链接")
	@GetMapping("/get/oss/url")
	public Result getUrl(String key) {
		return ok("", fileUploadService.getFileUrl(key));
	}

	@ApiOperation("单个oss文件下载")
	@GetMapping("/download")
	public void download(String ossKey, String fileName, HttpServletResponse response) {

		try {
			response.setContentType("application/doc");
			// 为防止 文件名出现乱码
			fileName = URLEncoder.encode(fileName,"UTF-8");
			response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
			//缓冲文件输出流
			BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
			fileDownloadService.download(ossKey, bos);

		} catch (IOException e) {
			log.error("download file error", e);
		}

	}

	@ApiOperation("多个oss文件打包zip下载")
	@PostMapping("/download/zip")
	public void downloadFilesForZip(@RequestBody List<OSSFileDownloadDTO> list, HttpServletResponse response) {

		try {
			response.setContentType("application/doc");
			response.addHeader("Content-Disposition", "attachment;filename=test.zip");
			//缓冲文件输出流
			BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
			fileDownloadService.downloadFilesForZip(list, bos);

		} catch (IOException e) {
			log.error("downloadFilesForZip error", e);
		}
	}

}
